package com.lanou3g.k12api.web.controller;

import com.lanou3g.k12api.entity.Course;
import com.lanou3g.k12api.utils.K12Result;
import com.lanou3g.k12api.utils.K12ResultUtils;
import com.lanou3g.k12api.utils.RandomEntityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Api(description = "课程管理")
@RestController
public class CourseController {

    @ApiOperation(value = "新增课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "课程ID（新增接口不可传ID）", paramType = "form"),
            @ApiImplicitParam(name = "name", value = "课程名称", required = true, example = "Python", paramType = "form"),
            @ApiImplicitParam(name = "cover", value = "封面图", required = true, example = "https://wx3.sinaimg" +
                    ".cn/large/007dOZvply1g2curtd7flj31hc0mz15p.jpg", paramType = "form"),
            @ApiImplicitParam(name = "cid", value = "课程分类ID", required = true, paramType = "form", example = "1"),
            @ApiImplicitParam(name = "tid", value = "课程类型ID", required = true, paramType = "form", example = "1"),
            @ApiImplicitParam(name = "price", value = "课程价格（整数类型，精确到分，如1299表示12.99元）", required = true, paramType =
                    "form", example = "98"),
            @ApiImplicitParam(name = "level", value = "课程难度等级（0为不设置，1为入门，2为初级，3为中级，4为高级，5为专业）", required = true,
                    paramType = "form", defaultValue = "0", allowableValues = "0,1,2,3,4,5"),
            @ApiImplicitParam(name = "sort", value = "课程排序值（升序）", paramType = "form", required = true, example = "1"),
            @ApiImplicitParam(name = "description", value = "描述", example = "这是一条测试课程", paramType = "form"),
            @ApiImplicitParam(name = "createTime", value = "创建时间（不用传）", paramType = "form"),
            @ApiImplicitParam(name = "updateTime", value = "修改时间（不用传）", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "状态（0 代表正常，1 代表停用）", required = true, defaultValue = "0",
                    allowableValues = "0,1", paramType = "form")
    })
    @PostMapping("/course")
    public K12Result create(Course course) {
        System.out.println(course);
        return K12ResultUtils.ok();
    }

    @ApiOperation(value = "通过ID删除课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要删除的ID", example = "1")
    })
    @DeleteMapping("/course/{id}")
    public K12Result delete(@PathVariable Long id) {
        System.out.println(id);
        return K12ResultUtils.ok();
    }

    @ApiOperation(value = "通过ID批量删除课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "要删除的ID（逗号分隔）", example = "1,2,3")
    })
    @DeleteMapping("/courses/{ids}")
    public K12Result delete(@PathVariable Long[] ids) {
        System.out.println(Arrays.toString(ids));
        return K12ResultUtils.ok();
    }

    @ApiOperation(value = "通过ID修改课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "课程ID", required = true, example = "1"),
            @ApiImplicitParam(name = "name", value = "课程名称", required = true, example = "Python", paramType = "form"),
            @ApiImplicitParam(name = "cover", value = "封面图", required = true, example = "https://wx3.sinaimg" +
                    ".cn/large/007dOZvply1g2curtd7flj31hc0mz15p.jpg", paramType = "form"),
            @ApiImplicitParam(name = "cid", value = "课程分类ID", required = true, paramType = "form", example = "1"),
            @ApiImplicitParam(name = "tid", value = "课程类型ID", required = true, paramType = "form", example = "1"),
            @ApiImplicitParam(name = "price", value = "课程价格（整数类型，精确到分，如1299表示12.99元）", required = true, paramType =
                    "form", example = "98"),
            @ApiImplicitParam(name = "level", value = "课程难度等级（0为不设置，1为入门，2为初级，3为中级，4为高级，5为专业）", required = true,
                    paramType = "form", defaultValue = "0", allowableValues = "0,1,2,3,4,5"),
            @ApiImplicitParam(name = "sort", value = "课程排序值（升序）", paramType = "form", required = true, example = "1"),
            @ApiImplicitParam(name = "description", value = "描述", example = "这是一条测试课程", paramType = "form"),
            @ApiImplicitParam(name = "createTime", value = "创建时间（不用传）", paramType = "form"),
            @ApiImplicitParam(name = "updateTime", value = "修改时间（不用传）", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "状态（0 代表正常，1 代表停用）", required = true, defaultValue = "0",
                    allowableValues = "0,1", paramType = "form")
    })
    @PutMapping("/course/{id}")
    public K12Result update(@PathVariable Long id, Course course) {
        System.out.println(id);
        System.out.println(course.getId());
        return K12ResultUtils.ok();
    }

    @ApiOperation(value = "通过ID获取课程", notes = "status：0 代表正常，1 代表停用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要获取的课程的ID", example = "1")
    })
    @GetMapping("/course/{id}")
    public K12Result findById(@PathVariable Long id) {
        Course course = RandomEntityUtils.random(Course.class);
        course.setId(id);
        course.setStatus(Math.random() >= 0.1 ? 0 : 1);
        return K12ResultUtils.ok(course);
    }

    @ApiOperation(value = "获取课程列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页数据量（建议不传）", defaultValue = "30"),
            @ApiImplicitParam(name = "keyword", value = "查询关键词"),
    })
    @GetMapping("/courses")
    public K12Result list(@RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "30") int size,
                          @RequestParam(defaultValue = "") String keyword) {
        System.out.println(page + "\t" + size + "\t" + keyword);
        List<Course> courses = RandomEntityUtils.random(Course.class, size);
        return K12ResultUtils.ok(courses);
    }
}
